package org.etnaframework.module.base.utils;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 带名称的线程工厂类，提供创建线程的功能
 *
 * @author jasyaf
 * @since 2023-12-05
 */
public class NamedThreadFactory implements ThreadFactory {

    /** 线程组 */
    protected final ThreadGroup group;

    /** 保证原子操作的整数 */
    protected final AtomicLong threadNumber = new AtomicLong(1);

    /** 名字前缀 */
    protected final String namePrefix;

    /** 默认优先级 */
    protected int priority = Thread.NORM_PRIORITY;

    /** 是否为守护线程 */
    protected boolean daemon = false;

    public NamedThreadFactory(String namePrefix, int priority, boolean daemon) {
        this(namePrefix);
        this.daemon = daemon;
        this.priority = priority;
    }

    public NamedThreadFactory(String namePrefix, int priority) {
        this(namePrefix);
        this.priority = priority;
    }

    public NamedThreadFactory(String namePrefix) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        this.namePrefix = namePrefix;
    }

    /**
     * 创建一个新的线程
     */
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r, namePrefix + Math.abs(threadNumber.getAndIncrement()), 0);
        t.setDaemon(daemon);
        t.setPriority(priority);
        return t;
    }

    public String getNamePrefix() {
        return namePrefix;
    }

    @Override
    public String toString() {
        return getNamePrefix();
    }
}
